package com.open.free.generator.project;

import com.open.free.generator.constant.TemplateConstants;
import com.open.free.generator.utils.FileIOUtils;
import com.open.free.generator.utils.FreemarkerUtils;
import com.open.free.generator.utils.JsonUtils;
import com.open.free.generator.vo.InitProjectVo;
import com.open.free.generator.vo.PackageClassVo;
import com.open.free.generator.vo.PropertiesVo;
import com.open.free.generator.vo.TemplateVo;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Faye.Wang
 * @version 1.0
 * @date 2020/12/19 14:40
 * @Description
 */
@Component
public class CoreClassGenerator {

    @Autowired
    private FreemarkerUtils freemarkerUtils;

    /**
     * java类模板生成
     * @param initProjectVo
     * @param corePackPatch
     * @param jsonFile
     * @throws IOException
     * @throws TemplateException
     */
    public void coreGenerator(InitProjectVo initProjectVo,String corePackPatch,String jsonFile) throws IOException, TemplateException {
        ClassPathResource resource = new ClassPathResource(jsonFile);

        String coreJsonData = IOUtils.toString(resource.getInputStream(), Charset.forName("UTF-8"));

        List<PackageClassVo> list = JsonUtils.toJsonList(coreJsonData,PackageClassVo.class);
        Map<String,Object> dataMaps = new HashMap<>();
        dataMaps.put("initProjectVo",initProjectVo);
        for(PackageClassVo pcv :list){
            if("Y".equals(initProjectVo.getModuleType()) && "interceptor".equals(pcv.getPackageName())){
                continue;
            }
            String targetPath = corePackPatch+File.separator+pcv.getPackageName();
            FileIOUtils.getFileExists(targetPath);
            for(TemplateVo tpl:pcv.getTemplate()){
                String content =  freemarkerUtils.processTemplateIntoString(tpl.getPath(),dataMaps);
                FileIOUtils.createFile(content,targetPath,tpl.getClassName()+TemplateConstants.JAVA_FILE_SUFFIX);
            }
        }
    }

    /**
     * 生成properties配置
     * @param resourcePath
     * @throws IOException
     * @throws TemplateException
     */
    public void propertiesGenerator(String resourcePath,InitProjectVo initProjectVo) throws IOException, TemplateException {
        ClassPathResource resource = new ClassPathResource(TemplateConstants.PROPERTIES_DATA_JSON);

        String coreJsonData = IOUtils.toString(resource.getInputStream(), Charset.forName("UTF-8"));

        List<PropertiesVo> list = JsonUtils.toJsonList(coreJsonData,PropertiesVo.class);
        Map<String,Object> dataMaps = new HashMap<>();
        dataMaps.put("initProjectVo",initProjectVo);
        for(PropertiesVo pcv :list){
            String content =  freemarkerUtils.processTemplateIntoString(pcv.getPath(),dataMaps);
            FileIOUtils.createFile(content,resourcePath,pcv.getProperties()+TemplateConstants.PROPERTIES_FILE_SUFFIX);
        }
        this.log4jGenerator(resourcePath);
    }

    /**
     * 日志文件
     * @param resourcePath
     * @throws IOException
     * @throws TemplateException
     */
    public void log4jGenerator(String resourcePath) throws IOException, TemplateException {
        String content =  freemarkerUtils.processTemplateIntoString(TemplateConstants.LOG4J_TEMPLATE,null);
        FileIOUtils.createFile(content,resourcePath,"log4j2-spring"+TemplateConstants.XML_FILE_SUFFIX);
    }

    public void baseModelCode(String packagePath,InitProjectVo initProjectVo,String resourcesPath)  throws IOException, TemplateException{
        ClassPathResource resource = new ClassPathResource(TemplateConstants.BASE_CODE_DATA_JSON);
        String baseJsonData = IOUtils.toString(resource.getInputStream(), Charset.forName("UTF-8"));
        List<PackageClassVo> list = JsonUtils.toJsonList(baseJsonData,PackageClassVo.class);
        Map<String,Object> dataMaps = new HashMap<>();
        dataMaps.put("initProjectVo",initProjectVo);

        for(PackageClassVo pcv :list){
            String targetPath = packagePath+File.separator+pcv.getPackageName();
            for(TemplateVo tpl:pcv.getTemplate()){
                String suffix = TemplateConstants.JAVA_FILE_SUFFIX;
                if("mapperXml".equals(pcv.getPackageName())){
                    targetPath = resourcesPath+File.separator+FileIOUtils.diskConvert(initProjectVo.getPackageName())+File.separator+"mapper";
                    suffix  =  TemplateConstants.XML_FILE_SUFFIX;
                }
                String content =  freemarkerUtils.processTemplateIntoString(tpl.getPath(),dataMaps);
                FileIOUtils.createFile(content,targetPath,tpl.getClassName()+suffix);
            }
        }
    }



}
